<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="姓名" prop="name">
      <el-input v-model="dataForm.name" placeholder="姓名"></el-input>
    </el-form-item>
    <el-form-item label="身份证号码" prop="idCard">
      <el-input v-model="dataForm.idCard" placeholder="身份证号码"></el-input>
    </el-form-item>
    <el-form-item label="联系电话" prop="phoneNumber">
      <el-input v-model="dataForm.phoneNumber" placeholder="联系电话"></el-input>
    </el-form-item>
    <el-form-item label="所属街道" prop="street">
      <el-input v-model="dataForm.street" placeholder="所属街道"></el-input>
    </el-form-item>
    <el-form-item label="所属社区" prop="community">
      <el-input v-model="dataForm.community" placeholder="所属社区"></el-input>
    </el-form-item>
    <el-form-item label="住址" prop="address">
      <el-input v-model="dataForm.address" placeholder="住址"></el-input>
    </el-form-item>
    <el-form-item label="救助次数" prop="rescueTimes">
      <el-input v-model="dataForm.rescueTimes" placeholder="救助次数"></el-input>
    </el-form-item>
    <el-form-item label="人口数" prop="populationNumber">
      <el-input v-model="dataForm.populationNumber" placeholder="人口数"></el-input>
    </el-form-item>
    <el-form-item label="所属区域" prop="region">
      <el-input v-model="dataForm.region" placeholder="所属区域"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          idCard: '',
          phoneNumber: '',
          street: '',
          community: '',
          address: '',
          rescueTimes: '',
          populationNumber: '',
          region: ''
        },
        dataRule: {
          name: [
            { required: true, message: '姓名不能为空', trigger: 'blur' }
          ],
          idCard: [
            { required: true, message: '身份证号码不能为空', trigger: 'blur' }
          ],
          phoneNumber: [
            { required: true, message: '联系电话不能为空', trigger: 'blur' }
          ],
          street: [
            { required: true, message: '所属街道不能为空', trigger: 'blur' }
          ],
          community: [
            { required: true, message: '所属社区不能为空', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '住址不能为空', trigger: 'blur' }
          ],
          rescueTimes: [
            { required: true, message: '救助次数不能为空', trigger: 'blur' }
          ],
          populationNumber: [
            { required: true, message: '人口数不能为空', trigger: 'blur' }
          ],
          region: [
            { required: true, message: '所属区域不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/generator/tbldifficultpersonnel/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.tblDifficultPersonnel.name
                this.dataForm.idCard = data.tblDifficultPersonnel.idCard
                this.dataForm.phoneNumber = data.tblDifficultPersonnel.phoneNumber
                this.dataForm.street = data.tblDifficultPersonnel.street
                this.dataForm.community = data.tblDifficultPersonnel.community
                this.dataForm.address = data.tblDifficultPersonnel.address
                this.dataForm.rescueTimes = data.tblDifficultPersonnel.rescueTimes
                this.dataForm.populationNumber = data.tblDifficultPersonnel.populationNumber
                this.dataForm.region = data.tblDifficultPersonnel.region
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/tbldifficultpersonnel/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'idCard': this.dataForm.idCard,
                'phoneNumber': this.dataForm.phoneNumber,
                'street': this.dataForm.street,
                'community': this.dataForm.community,
                'address': this.dataForm.address,
                'rescueTimes': this.dataForm.rescueTimes,
                'populationNumber': this.dataForm.populationNumber,
                'region': this.dataForm.region
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
